package com.levy.utils;

import java.io.*;
import java.math.BigInteger;

/**
 * Created by levy on 2019/7/11.
 */
public class HexUtils {

    /**
     * 字节数组转成16进制字符串
     * @param bytes
     * @return
     */
    public static String bytesToHexString(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            String hex = Integer.toHexString(0xFF & bytes[i]);
            if (hex.length() == 1) {
                sb.append('0');
            }
            sb.append(hex);
        }
        return sb.toString();
    }

    /**
     * 把字符串转换为 2位 为一组的数组
     * @param s
     * @return
     */
    public static String[] bytesToHexArray(String s) {
        String[] strArray = new String[s.length() / 2];
        String tmpStr = "";
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            tmpStr += c;
            if (i != 0 && i % 2 != 0) {
                strArray[i / 2] = tmpStr;
                tmpStr = "";
            }

        }
        return strArray;
    }

    /**
     * 16进制字符串转为整型
     * @param str
     * @return
     */
    public static Long HexStrToLong(String str) {
        BigInteger bigInteger = new BigInteger(str, 16);
        return bigInteger.longValue();
    }


    /**
     * 打印出文件的16进制字符串
     * @param path
     */
    public static void printHexStr(String path) {
        try (FileInputStream fis = new FileInputStream(path);
             DataInputStream dis = new DataInputStream(fis)
        ) {
            byte[] bytes = new byte[dis.available()];
            dis.read(bytes);
            String s = bytesToHexString(bytes);
            HexPrint(s);


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 打印出字节数组的16进制字符串
     * @param bytes
     */
    public static void printHexStr(byte[] bytes) {
        String s = bytesToHexString(bytes);
        HexPrint(s);
    }

    /**
     * 按照16进制工具查看文件的 16进制格式 打印出16进制字符串
     * @param s
     */
    private static void HexPrint(String s) {
        int length = s.length();
        for (int i = 0; i < length; i++) {
            if (i == 0) {
                System.out.print(s.charAt(i));
                continue;
            } else if (i % 40 == 0) {
                System.out.println();
            } else if (i % 2 == 0) {
                System.out.print(" ");
            }
            System.out.print(s.charAt(i));
        }
        System.out.println();
    }

    /**
     * 从流中打印出16进制字符串
     * @param is
     */
    public static void printHexStr(InputStream is) {
        try (DataInputStream dis = new DataInputStream(is)
        ) {
            byte[] bytes = new byte[dis.available()];
            dis.read(bytes);
            String s = bytesToHexString(bytes);
            HexPrint(s);


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 反转数组
     * @param arr
     * @param begin
     * @param end
     */
    public static void reverse(String[] arr, int begin, int end) {
        while(end > begin) {
            String temp = arr[begin];
            arr[begin] = arr[end];
            arr[end] = temp;

            begin++;
            end--;
        }
    }

    public static void main(String[] args) {

        printHexStr("abcdef".getBytes());

    }
}

